package handler

import (
	"encoding/json"
	"github.com/gin-gonic/gin"
	"logi-hub/cache"
	"logi-hub/model"
	"logi-hub/pkg"
	"strconv"
	"time"
)

func Login(c *gin.Context) {
	username := c.PostForm("username")
	password := c.PostForm("password")
	if username == "" || password == "" {
		c.JSON(200, gin.H{
			"code": 400,
			"msg":  "不能为空",
		})
		return
	}
	redis_key := "redis_" + username
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Int()
	if cacheData == 3 {
		c.JSON(200, gin.H{
			"code": 400,
			"msg":  "密码错误3次，禁止登录2小时",
		})
		return
	}
	var user model.User
	model.DB.Where("username = ?", username).First(&user)
	if user.ID == 0 {
		c.JSON(200, gin.H{
			"code": 400,
			"msg":  "用户不存在",
		})
		return
	}
	if user.Password != pkg.Md5(password) {
		cache.Rdb.Incr(cache.Ctx, redis_key)
		if cacheData >= 2 {
			cache.Rdb.Set(cache.Ctx, redis_key, 3, time.Hour*2)
			c.JSON(200, gin.H{
				"code": 400,
				"msg":  "密码错误3次",
			})
			return
		}
		c.JSON(200, gin.H{
			"code": 400,
			"msg":  "密码错误",
		})
		return

	}
	c.JSON(200, gin.H{
		"code": 400,
		"msg":  "登陆成功",
		"data": user,
	})
	return
}
func UserRole(c *gin.Context) {
	//SELECT
	//*
	//	FROM
	//roles
	//LEFT JOIN user_roles ON user_roles.uid=roles.id
	var role model.Role
	model.DB.Table("roles").
		Joins("LEFT JOIN user_roles ON user_roles.uid=roles.id").Debug().Find(&role)
	c.JSON(200, gin.H{
		"code": 200,
		"data": role,
		"msg":  "success",
	})
	return
}
func UserRoles(c *gin.Context) {
	id := c.PostForm("id")
	//SELECT
	//*
	//	FROM
	//roles
	//LEFT JOIN user_roles ON user_roles.uid=roles.id
	//WHERE user_roles.uid=1
	var role model.Role
	model.DB.Table("roles").
		Joins("LEFT JOIN user_roles ON user_roles.uid=roles.id").
		Where(" user_roles.uid= ?", id).Debug().Find(&role)
	c.JSON(200, gin.H{
		"code": 200,
		"data": role,
		"msg":  "success",
	})
	return

}
func RoleNode(c *gin.Context) {
	//SELECT
	//*
	//	FROM
	//nodes
	//LEFT JOIN role_nodes ON role_nodes.rid=nodes.id
	//WHERE role_nodes.rid=1
	id := c.PostForm("id")
	var node model.Node
	model.DB.Table("nodes").
		Joins("LEFT JOIN role_nodes ON role_nodes.rid=nodes.id").
		Where(" role_nodes.rid = ?", id).Debug().Find(&node)
	c.JSON(200, gin.H{
		"code": 200,
		"data": node,
		"msg":  "success",
	})
	return

}
func PeroAdd(c *gin.Context) {
	peroname := c.PostForm("peroname")
	perocode := c.PostForm("perocode")
	data := model.Pero{
		PeroName: peroname,
		PeroCode: perocode,
	}
	model.DB.Create(&data)
	c.JSON(200, gin.H{
		"code": 200,
		"data": data,
		"msg":  "添加成功",
	})
	return
}
func PerOrderAdd(c *gin.Context) {
	peroid, _ := strconv.Atoi(c.PostForm("peroid"))
	ordercode := c.PostForm("ordercode")
	goodnum, _ := strconv.Atoi(c.PostForm("goodnum"))
	orderprice, _ := strconv.Atoi(c.PostForm("orderprice"))
	data := model.PerOrder{
		PeroId:     peroid,
		OrderCode:  ordercode,
		GoodNum:    goodnum,
		OrderPrice: orderprice,
	}
	model.DB.Create(&data)
	c.JSON(200, gin.H{
		"code": 200,
		"data": data,
		"msg":  "添加成功",
	})
	return
}
func PerOrderList(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	size, _ := strconv.Atoi(c.DefaultQuery("size", "5"))
	offset := (page - 1) * size
	//SELECT
	//per_orders.id AS OrderId,
	//	per_orders.order_code AS OrderCode,
	//	per_orders.good_num AS GoodNum,
	//	per_orders.order_price AS OrderPrice,
	//	peros.pero_name AS PeroName,
	//	peros.pero_code AS PeroCode
	//FROM
	//peros
	//LEFT JOIN per_orders ON peros.id=per_orders.pero_id
	var PerOrders []model.PerOrderss
	redis_key := "redis_error" + strconv.Itoa(page)
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		model.DB.Table("peros").Select("per_orders.id AS OrderId",
			"per_orders.order_code AS OrderCode", "per_orders.good_num AS GoodNum",
			"per_orders.order_price AS OrderPrice", "peros.pero_name AS PeroName",
			"peros.pero_code AS PeroCode").
			Joins("LEFT JOIN per_orders ON peros.id=per_orders.pero_id").
			Offset(offset).Limit(size).Debug().Find(&PerOrders)
		jsonData, _ := json.Marshal(PerOrders)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &PerOrders)

	}
	var total int64
	model.DB.Table("peros").Count(&total)
	c.JSON(200, gin.H{
		"code":  200,
		"data":  PerOrders,
		"msg":   "展示成功",
		"total": total,
	})
	return
}
func PerOrderDetail(c *gin.Context) {
	id := c.PostForm("id")
	//SELECT
	//per_orders.id AS OrderId,
	//	per_orders.order_code AS OrderCode,
	//	per_orders.good_num AS GoodNum,
	//	per_orders.order_price AS OrderPrice,
	//	peros.pero_name AS PeroName,
	//	peros.pero_code AS PeroCode
	//FROM
	//peros
	//LEFT JOIN per_orders ON peros.id=per_orders.pero_id
	//WHERE peros.id=1
	var PerOrders []model.PerOrderss
	redis_key := "redis_error_detail" + id
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		model.DB.Table("peros").Select("per_orders.id AS OrderId",
			"per_orders.order_code AS OrderCode", "per_orders.good_num AS GoodNum",
			"per_orders.order_price AS OrderPrice", "peros.pero_name AS PeroName",
			"peros.pero_code AS PeroCode").
			Joins("LEFT JOIN per_orders ON peros.id=per_orders.pero_id").
			Where(" peros.id=?", id).Debug().Find(&PerOrders)
		jsonData, _ := json.Marshal(PerOrders)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &PerOrders)
	}
	var total int64
	model.DB.Table("peros").Count(&total)
	c.JSON(200, gin.H{
		"code":  200,
		"data":  PerOrders,
		"msg":   "展示成功",
		"total": total,
	})
	return
}
